// pages/mypage/allorder/allorder.js
const request = require('@/utils/request.js')
const constants = require('@/utils/khyConstants.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabValue: 0,
    dataList: [],
    // pageNo: 1,
    // pageSize: constants.pageSize,
    // total: 0,
    stickyProps: {
        zIndex: 2,
    },
    currentOrder: null,
    showWarnConfirm: false,
    packageStateList: getApp().globalData.dictData['packageState'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let value = getApp().globalData.pageData[constants.page_allorder]
    this.getPageData(value)
  },

  getPageData(tabValue) {
    let user = getApp().getUserInfo()
    request.get('/UserController/getOrderByMyType', {type: tabValue, userNo: user.userNo}).then(res => {
      if (res.data.code == constants.resultSuccessCode
        && res.data.result) {
          let data = res.data.result || []
          this.setData({
            dataList: data,
            tabValue: tabValue
          })
      }
    })
  },

  onTabsChange(event) {
    this.getPageData(event.detail.value)
  },

  orderinfo(e) {
    let order = e.currentTarget.dataset.order
    if (order) {
      getApp().globalData.pageData[constants.page_orderinfo] = order
      wx.navigateTo({
        url: '/pages/orderinfor/orderinfor',
      })
    }
  },

  //作废单据
  cancelClick(e) {
    let order = e.currentTarget.dataset.order
    if (order) {
      this.setData({
        currentOrder: order,
        showWarnConfirm: true
      })
    }
    
  },

  confirmCancel(e) {
    let currentOrder = this.data.currentOrder
    if (currentOrder) {
      console.log("order: " + currentOrder.packageState)
      request.post('/UserController/cancelPackage', currentOrder).then(res => {
        if (res.data.code == constants.resultSuccessCode) {
          let value = getApp().globalData.pageData[constants.page_allorder]
          this.getPageData(value)
          wx.showToast({
            title: '作废成功',
            icon: 'success',
            duration: 2000
          })
        }
      })
    }
    this.setData({
      showWarnConfirm: false
    })
  },

  closeDialog(e) {
    this.setData({
      showWarnConfirm: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})